//
//  UpMenuView.cpp
//  FruitCrush
//
//  Created by gaoyu on 15/7/28.
//
//

#include "UpMenuView.h"
#include "HerosInfoScreen.h"
#include "MarketMainView.h"
#include "SystemMainView.h"
USING_NS_CC;

#define HERO_MENU_X    0
#define HERO_MENU_Y    0
#define MENU_Y_PAD     50
#define CONTROL_MENU_Y (HERO_MENU_Y-25)
#define CLOSE_X_PAD    (60)


typedef enum {
    ZBg = 0,
    ZMenu,
    
} Zindex;

typedef enum {
    TagMenu = 1,
    TagHerosMenu,
    TagMarketMainMenu,
    TagAchieveMapMenu,
    TagControlLeftMapMenu,
    TagControlRightMapMenu,
} MenuTag;

UpMenuView::UpMenuView()
:m_isActioning(false)
{
}

bool UpMenuView::init()
{
    if (!Layer::init()) {
        return false;
    }
    menuInit();
    
    return true;
}

void UpMenuView::menuInit()
{
    auto menuItemHeros = MenuItemImage::create("MenuHerosNormal.png", "MenuHerosSelected.png","MenuHerosNormal.png", CC_CALLBACK_1(UpMenuView::menuCallback, this));
    menuItemHeros->setTag(TagHerosMenu);
    menuItemHeros->setAnchorPoint(Vec2::ZERO);
    menuItemHeros->setPosition(Point(HERO_MENU_X,HERO_MENU_Y));
    
    auto menuItemMarket = MenuItemImage::create("MenuShopNormal.png", "MenuShopSelected.png","MenuShopNormal.png", CC_CALLBACK_1(UpMenuView::menuCallback, this));
    menuItemMarket->setTag(TagMarketMainMenu);
    menuItemMarket->setAnchorPoint(Vec2::ZERO);
    menuItemMarket->setPosition(Point(HERO_MENU_X,HERO_MENU_Y+MENU_Y_PAD));

    auto menuItemAchieve = MenuItemImage::create("AchieveMapMenu.png", "AchieveMapMenuSelected.png","AchieveMapMenu.png", CC_CALLBACK_1(UpMenuView::menuCallback, this));
    menuItemAchieve->setTag(TagAchieveMapMenu);
    menuItemAchieve->setAnchorPoint(Vec2::ZERO);
    menuItemAchieve->setPosition(Point(HERO_MENU_X,HERO_MENU_Y+MENU_Y_PAD*2));

    auto menuItemControlLeft = MenuItemImage::create("MenuLeftArrow.png", "MenuLeftArrowSelected.png","MenuLeftArrow.png", CC_CALLBACK_1(UpMenuView::menuCallback, this));
    menuItemControlLeft->setTag(TagControlLeftMapMenu);
    menuItemControlLeft->setAnchorPoint(Vec2::ZERO);
    menuItemControlLeft->setPosition(Point(HERO_MENU_X,CONTROL_MENU_Y));

    auto menuItemControlRight = MenuItemImage::create("MenuRightArrow.png", "MenuRightArrowSelected.png","MenuRightArrow.png", CC_CALLBACK_1(UpMenuView::menuCallback, this));
    menuItemControlRight->setTag(TagControlRightMapMenu);
    menuItemControlRight->setAnchorPoint(Vec2::ZERO);
    menuItemControlRight->setPosition(Point(HERO_MENU_X,CONTROL_MENU_Y));
    menuItemControlRight->setVisible(false);

    auto menu = Menu::create(menuItemHeros, menuItemMarket, menuItemAchieve,menuItemControlLeft,menuItemControlRight, NULL);
    menu->setAnchorPoint(Vec2::ZERO);
    menu->setPosition(Vec2::ZERO);
    menu->setTag(TagMenu);
    addChild(menu,ZMenu);
}

void UpMenuView::menuCallback(Ref *sender)
{
    MenuItemImage *seletedMenu = (MenuItemImage*)sender;
    if(seletedMenu->getTag() == TagHerosMenu){
    	auto scene = HerosInfoScreen::createScene();
        Director::getInstance()->pushScene(scene);
    }else if(seletedMenu->getTag() == TagMarketMainMenu){
    	auto scene = MarketMainView::createScene();
        Director::getInstance()->pushScene(scene);
    }else if(seletedMenu->getTag() == TagAchieveMapMenu){
    	auto scene = SystemMainView::createScene();
        Director::getInstance()->pushScene(scene);
    }else if(seletedMenu->getTag() == TagControlLeftMapMenu){
        menuCloseOrOpen(true);
    }else if(seletedMenu->getTag() == TagControlRightMapMenu){
    	menuCloseOrOpen(false);
    }
}

void UpMenuView::menuCloseOrOpen(bool isClose)
{
	if(m_isActioning) return;
	Menu* menu = (Menu*)this->getChildByTag(TagMenu);
	MenuItemImage* menuItemHeros = (MenuItemImage*)menu->getChildByTag(TagHerosMenu);
	MenuItemImage* menuItemMarket = (MenuItemImage*)menu->getChildByTag(TagMarketMainMenu);
	MenuItemImage* menuItemAchieve = (MenuItemImage*)menu->getChildByTag(TagAchieveMapMenu);
	MenuItemImage* menuItemRight = (MenuItemImage*)menu->getChildByTag(TagControlRightMapMenu);
	MenuItemImage* menuItemLeft = (MenuItemImage*)menu->getChildByTag(TagControlLeftMapMenu);
	float time = 0.5;
	m_isActioning = true;
    if(isClose){
    	menuItemLeft->setVisible(false);
    	menuItemRight->setVisible(true);
    	menuItemHeros->runAction(Sequence::create(
        		                          MoveTo::create(time, Point(HERO_MENU_X-CLOSE_X_PAD,HERO_MENU_Y)),
                                          NULL));
    	menuItemMarket->runAction(Sequence::create(
        		                          MoveTo::create(time, Point(HERO_MENU_X-CLOSE_X_PAD,HERO_MENU_Y+MENU_Y_PAD)),
                                          NULL));
    	menuItemAchieve->runAction(Sequence::create(
        		                          MoveTo::create(time, Point(HERO_MENU_X-CLOSE_X_PAD,HERO_MENU_Y+MENU_Y_PAD*2)),
                                          CallFuncN::create(CC_CALLBACK_1(UpMenuView::actionEndCallback, this)),
                                          NULL));
    }else{
    	menuItemLeft->setVisible(true);
    	menuItemRight->setVisible(false);
    	menuItemHeros->runAction(Sequence::create(
        		                          MoveTo::create(time, Point(HERO_MENU_X,HERO_MENU_Y)),
                                          NULL));
    	menuItemMarket->runAction(Sequence::create(
        		                          MoveTo::create(time, Point(HERO_MENU_X,HERO_MENU_Y+MENU_Y_PAD)),
                                          NULL));
    	menuItemAchieve->runAction(Sequence::create(
        		                          MoveTo::create(time, Point(HERO_MENU_X,HERO_MENU_Y+MENU_Y_PAD*2)),
                                          CallFuncN::create(CC_CALLBACK_1(UpMenuView::actionEndCallback, this)),
                                          NULL));
    }
}

void UpMenuView::actionEndCallback(Node *node)
{
	m_isActioning = false;

}
